package com.jse.view;

import java.io.File;
import java.nio.file.LinkOption;
import java.nio.file.Paths;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.json.Json;
import org.nutz.lang.Files;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.view.ForwardView;
import org.nutz.mvc.view.JspView;
import org.nutz.mvc.view.RawView;
import org.nutz.mvc.view.ServerRedirectView;
import org.springframework.web.servlet.ModelAndView;

import jdk.nashorn.api.scripting.ScriptObjectMirror;

public class JseView {

	public static Object view(String jjs, String func, Object obj, HttpServletRequest req, HttpServletResponse resp)
			throws Throwable {
		if (req.getAttribute("_httpurl") != null) {
			String httpurl = req.getAttribute("_httpurl").toString();
			if (httpurl.contains("http")) {
				new ServerRedirectView(httpurl).render(req, resp, obj);
				return null;
			} else {
				new ServerRedirectView("/" + httpurl).render(req, resp, obj);
				return null;
			}
		} else if (obj instanceof String) {
			String rtv = obj.toString().trim();
			if (rtv.startsWith("jsp:")) {
				new JspView("/" + rtv.substring(4)).render(req, resp, obj);
				return null;
			} else if (rtv.startsWith(">>:")) {
				new ServerRedirectView("/" + rtv.substring(3)).render(req, resp, obj);
				return null;
			} else if (rtv.startsWith("->:")) {
				new ForwardView("/" + rtv.substring(3)).render(req, resp, obj);
				return null;
			} else if (rtv.startsWith("http://") || rtv.startsWith("https://")) {
				new ServerRedirectView(rtv).render(req, resp, obj);
				return null;
			}
		} else if (obj instanceof File) {
			File f=(File) obj;
			String suffix = Files.getSuffixName(f.getName());
			if (Lang.contains(Lang.array("png", "jpg", "webp", "xml", "json", "txt"), suffix)) {
				new RawView(suffix).render(req, resp, obj);
				return null;
			} else {
				new RawView("application/octet-stream").render(req, resp, obj);
				return null;
			}
		} 
		if (obj instanceof ScriptObjectMirror) {// 针对jjs的[object Array]类型转换
			ScriptObjectMirror som = ScriptObjectMirror.class.cast(obj);
			if (som.toString().equals("[object Array]")) {
				obj = Json.fromJson(ArrayList.class, Json.toJson(som));
			} else if (som.toString().equals("[object Object]")) {
				obj = NutMap.WRAP(som);
			}
		}
		StringBuffer html = new StringBuffer(jjs);
		if (req.getAttribute("func") != null) {
			html.append("/").append(req.getAttribute("func").toString());
		} else {
			html.append(Strings.isEmpty(func) ? "" : "/".concat(func));
		}

		String path = req.getServletContext().getRealPath("");
		if (java.nio.file.Files.exists(Paths.get(path, html.toString().concat(".html")),
				LinkOption.NOFOLLOW_LINKS)) {// .html不存在
			ModelAndView model = new ModelAndView(html.toString());
			model.addObject("obj", obj);
			return model;
		}else if (obj instanceof Throwable) {
			new RawView("text/plain").render(req, resp, obj);
			return null;
		}
		return obj;
	}
}
